#!/usr/bin/env python

import requests
import random
import pdb
import sys

def testUserExists():
    random.seed()
    numuids = 100
    for step in range(2000000000, 3000000000, 100000000):
        uids = [random.randint(step+1, step+100000000) for i in range(0, numuids)]
        nummiss = 0
        for uid in uids:
            print("Requesting ", uid, end=" ", flush=True)
            while True:
                try:
                    ret = requests.head("http://weibo.com/u/" + str(uid), headers = headers,
                            allow_redirects = False)
                    break
                except requests.RequestException:
                    continue
            #print(ret.headers)
            if ret.status_code == 302:
                if "http://weibo.com/sorry" in ret.headers["Location"]:
                    print("Miss")
                    nummiss += 1
                else:
                    print("Hit ", end = " ", flush=True)
                    print("http://weibo.com" + ret.headers["Location"])
            else:
                print("Hit ", ret.url)
        print("\n\tWe test ", numuids, " users, got ", numuids - nummiss, \
                "real users, hit rate is {:.2%}".format(((numuids - nummiss)/numuids)))
        print("\tUid window: [", step+1, "~", step+100000000, "]\n")
   

if __name__=="__main__":
    headers = {
            "Host": "weibo.com",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": 1,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "DNT": 1,
            "Accept-Encoding": "gzip, deflate, sdch",
            "Accept-Language": "en-US,en;q=0.8",
            "Cookie": "SUB=_2A2564gw6DeTxGeNJ6FoU8i_FzD2IHXVZlnryrDV8PUNbmtBeLWHAkW9zFGPLeaHVEStwdN44oZwrKiwevg..;",
            }
    testUserExists()
    sys.exit()
    random.seed()
    begin, end = (200000000, 300000000)
    numuids = 100 
    uids = [random.randint(begin, end) for i in range(0, numuids)]
    nummiss = 0
    for uid in uids:
        print("Requesting ", uid, end=" ", flush=True)
        while True:
            try:
                ret = requests.head("http://weibo.com/u/" + str(uid), headers = headers,
                        allow_redirects = False)
                break
            except requests.RequestException:
                continue
        #print(ret.headers)
        if ret.status_code == 302:
            if "http://weibo.com/sorry" in ret.headers["Location"]:
                print("Miss")
                nummiss += 1
            else:
                print("Hit ", end = " ", flush=True)
                print("http://weibo.com" + ret.headers["Location"])
        else:
            print("Hit ", ret.url)
    print("\n\tWe test ", numuids, " users, got ", numuids - nummiss, \
            "real users, hit rate is {:.2%}".format(((numuids - nummiss)/numuids)), "\n")

